﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StuManagementSystem
{
    public partial class frm_AddStuInfo : Form
    {
        /// <summary>
        /// 数据库操作函数
        /// </summary>
        DbContext db = new DbContext();
        public frm_AddStuInfo()
        {
            InitializeComponent();
        }
        #region 窗体预加载
        /// <summary>
        /// 窗体预加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frm_AddStuInfo_Load(object sender, EventArgs e)
        {
            //加载年纪数据
            var sql = @"
                    SELECT -1 StuGradeId,'-请选择-' StuGradeName
                    union
                    SELECT * FROM dbo.StuGrade;
                    SELECT TOP 1 StuCode FROM dbo.StuInfo ORDER BY StuCode DESC";
            DataSet ds = db.ExecDataSet(sql);
            //通过代码设置学号文本框的数据
            this.txt_StuCode.Text = (Convert.ToInt32(ds.Tables[1].Rows[0][0]) + 1).ToString();
            var dt = ds.Tables[0];
            this.cbo_StuGrade.DisplayMember = "StuGradeName";
            this.cbo_StuGrade.ValueMember = "StuGradeId";
            this.cbo_StuGrade.DataSource = dt;
            this.txt_StuPwd.Focus();
        }
        #endregion
  
      
        #region 提交
        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            if (CheckData() == false)
                return;
            #region 获取页面数据
            var v1 = this.txt_StuCode.Text;
            var v2 = this.txt_StuPwd.Text;
            var v3 = this.txt_StuName.Text;
            var v4 = -1;
            if (this.rdo_StuSexMan.Checked)
                v4 = 1;
            else
                v4 = 2;
            var v5 = Convert.ToInt32(this.cbo_StuGrade.SelectedValue);
            var v6 = this.txt_StuTelephone.Text;
            var v7 = this.txt_StuAddress.Text;
            var v8 = this.ckl_StuBirthDay.Value;
            var v9 = this.txt_StuEmail.Text;
            #endregion

            #region 构造SQL语句
            var sql = string.Format(@"
INSERT  dbo.StuInfo
(
    StuCode,
    StuPwd,
    StuName,
    StuSexId,
    StuGradeId,
    StuTelephone,
    StuAddress,
    StuBirthDay,
    StuEmail
)
VALUES
(   {0},         -- StuCode - int
    N'{1}',       -- StuPwd - nvarchar(50)
    N'{2}',       -- StuName - nvarchar(50)
    {3},         -- StuSexId - int
    {4},         -- StuGradeId - int
    '{5}',        -- StuTelephone - varchar(50)
    N'{6}',       -- StuAddress - nvarchar(50)
    '{7}', -- StuBirthDay - datetime
    '{8}'         -- StuEmail - varchar(50)
    )", v1, v2, v3, v4, v5, v6, v7, v8, v9);
        #endregion

            #region 提交数据
            if (db.ExecNonQuery(sql))
                MessageBox.Show("提交成功");
            else
                MessageBox.Show("添加失败");
            #endregion
        }
        #endregion

        #region 提交验证私有方法
        /// <summary>
        /// 验证用户输入是否合法
        /// </summary>
        /// <returns>true表示成功</returns>
        private bool CheckData()
        {
            if (string.IsNullOrWhiteSpace(this.txt_StuPwd.Text))
            {
                MessageBox.Show("密码不能为空");
                this.txt_StuPwd.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_StuPwds.Text))
            {
                MessageBox.Show("确认密码不能为空");
                this.txt_StuPwds.Focus();
                return false;
            }
            if (!this.txt_StuPwd.Text.Equals(this.txt_StuPwds.Text))
            {
                MessageBox.Show("两次密码不一致");
                this.txt_StuPwds.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_StuName.Text))
            {
                MessageBox.Show("姓名不能为空");
                this.txt_StuName.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.cbo_StuGrade.Text) || this.cbo_StuGrade.Text.Equals("-请选择-"))
            {
                MessageBox.Show("请选择年级");
                this.cbo_StuGrade.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_StuTelephone.Text))
            {
                MessageBox.Show("电话不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_StuAddress.Text))
            {
                MessageBox.Show("地址不能为空");
                this.txt_StuAddress.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_StuEmail.Text))
            {
                MessageBox.Show("Email不能为空");
                this.txt_StuEmail.Focus();
                return false;
            }
            return true;
        }
        #endregion

        #region 清除按钮
        /// <summary>
        /// 清除所有选项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Clear_Click(object sender, EventArgs e)
        {
            foreach (Control a in lbl_YongHuZhuCiXinXin.Controls)
            {
                //清除groupBox1中的文本框
                if (a is TextBox && a != txt_StuCode)
                {
                    a.Text = "";
                }
            }
            foreach (Control a in lbl_YongHuJiBenXinXi.Controls)
            {
                //清除groupBox2中的文本框和多选框
                if (a is TextBox || a is ComboBox)
                {
                    a.Text = "";
                }
                //女的单选框
                if (a == rdo_StuSexWoMan)
                {
                    this.rdo_StuSexMan.Checked = true;
                }
            }
            //日期控件
            this.ckl_StuBirthDay.Value = DateTime.Now;
        }
        #endregion


    }
}
